<template>
  <div>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-button type="primary" @click="show(0)">新增分类信息</el-button>
        </el-form-item>
      </el-form>
    </el-col>
    <el-button @click="find()">click</el-button>
    <el-card>
      <tree-table
          ref="multipleTable"
          :border="true"
          :show-index="true"
          :expand-type="false"
          index-text="索引"
          @selection-change="selsChange"
          :selection-type="true"
          :data="itemcat"
          :columns="columns"
      >
        <!--操作的作用域插槽-->
        <template slot="operate" scope="scope">
          <el-button size="mini" type="primary" @click="show(scope.row)" icon="el-icon-edit">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDel(scope.$index, scope.row)" icon="el-icon-delete">删除</el-button>
        </template>
      </tree-table>
    </el-card>
    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" :disabled="this.sels.length===0" @click="batchRemove">批量删除</el-button>
      <el-pagination layout="prev, pager, next" :current-page="pageNum" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog :title="title" width="600px" :visible.sync="editFormVisible" :close-on-click-modal="false">

      <el-form :model="showItemCat" label-width="120px" :rules="editFormRules" ref="editForm">
        <!--        <template scope="scope">-->
        <el-form-item label="上级商品分类">

          <el-cascader :props="{ checkStrictly: true,label:'label',value:'value' }" v-model="showItemCat.parentId"  :options="tree" clearable></el-cascader>

        </el-form-item>
        <!--        </template>-->
      </el-form>

      <el-form :model="showItemCat" label-width="120px" :rules="editFormRules" ref="editForm">
        <el-form-item label="商品分类名称" prop="name">
          <el-input v-model="showItemCat.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>

      <el-form :model="itemcat" label-width="120px" :rules="editFormRules" ref="editForm">
        <el-form-item label="类型模板" prop="typeId">
          <el-select v-model="showItemCat.typeId" clearable placeholder="请选择">
            <el-option
                v-for="item in templateList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>
    <!--  树形控件 -->
    <el-dialog
        title="提示"
        :visible.sync="editFormVisible2"
        width="30%"
        :before-close="handleClose">
      <el-tree :data="treeById" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
  </span>
    </el-dialog>


  </div>

</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      // 商品分类的数据列表
      temData: [],
      showItemCat: [{
        id:null,
        name: '',
        typeId: null,
        parentId: []
        // parentPath:''
      }],
      props: [{
        label: '',
        value:null
      }],
      tree: [],
      treeById: [],
      ids: [],
      title: '',
      filters: {
        keyword: ''
      },
      templateList: [],
      itemcat:[{
        id:null,
        parentId:null,
        name: "",
        typeId:null,
        parentPath: ''
      }],
      total: 0,
      pageNum: 1,
      pageSize:10,
      listLoading: false,
      sels: [],//列表选中列
      editFormVisible: false,//编辑界面是否显示
      editFormVisible2: false,//编辑界面是否显示
      editLoading: false,
      editFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },
      // 为table指定列的定义
      columns: [
        {
          label: "分类名称",
          prop: "name",
          width: "400px",
        },
        {
          label: "类型ID",
          prop: "typeId",
          minWidth: "50px",
        },
        {
          label: '操作',
          type: 'template', // 表示：把当前列定义为模板列
          template: 'operate' // 表示当前这列使用的模板名称
        }
      ],
    };
  },

  methods: {
    showTree(row) {
      this.editFormVisible2 = true;
      axios.get("/itemcat/queryTreeById/"+row.id).then(resp=>{
        console.log(resp);
        this.treeById = resp.data.data;
      }).catch(resp=>{

      })
    },
    //删除表单
    deleteRow(index) {
      this.showitemcat.customAttributeItems.splice(index);
    },
    //增加表单
    addForm() {
      var o = {};
      this.showitemcat.customAttributeItems.push(o);
    },
    //获取模版列表
    queryAllTemplate() {
      axios.get("/template/queryAll").then(resp=>{
        // console.log(resp.data.data);
        this.templateList = resp.data.data;
      }).catch(resp=>{

      })
    },
    //获取分类列表
    queryBaran(){
      // 准备参数
      let params = {
        pageSize: this.pageSize, //页面大小
        pageNum: this.pageNum
      }
      //页面展示
      this.$http.post("/itemcat/queryPage",params)
          .then(resp=>{
            console.log(resp);
            this.itemcat = resp.data.data.list;
            this.total = resp.data.data.total;
          }).catch(resp=>{
        alert("网络异常，稍后重试");
      })
      //查询树结构
      axios.get("/itemcat/queryTree").then(resp => {
        // console.log(resp);
        this.tree = resp.data.data;
      }).catch(resp => {

      });
    },
    //删除
    handleDel: function (index, row) {
      // console.log(row);
      this.$confirm('确认删除该记录吗?', '温馨提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/itemcat/deleteInfo/"+row.id)
            .then(resp=>{
                  //去除忙等框
                  if (resp.data.code==="1") {
                    this.editFormVisible = false;
                    this.$message({
                      message: '删除成功',
                      type: 'success'
                    });
                    this.listLoading = false;
                    this.queryBaran();
                  }
                }

            )
            .catch(result=>{
              //去除忙等框
              this.listLoading = false;
              //提示
              this.$message({
                message: '系统错误！',
                type: 'error'
              });
            })

      }).catch(() => {

      });
    },
    //显示编辑界面
    show (row) {
      console.log(row);
      this.editFormVisible = true;
      this.temData = Object.assign({}, this.showItemCat);

      // this.showItemCat.parentId = temp;
      // console.log(temp);
      if (row === 0) {
        this.showItemCat = {
          id: null,
          name: '',
          typeId: null,
          parentId: []
        };
        this.title = "新增";
      } else {
        this.title = "编辑";
      }
      if (row !== 0) {
        var temp = [];
        var ids = row.parentPath.split("/");
        for (let i = 1; i < ids.length; i++) {
          temp.push(parseInt(ids[i]));
        }
        console.log(temp);
        this.showItemCat.id = row.id;
        this.showItemCat.parentId = temp;
        this.showItemCat.name = row.name;
        this.showItemCat.typeId = row.typeId;
        // axios.get("/itemcat/queryById/"+row.id).then(resp=>{
        //   // console.log(resp);
        //   this.showItemCat = resp.data.data;
        //   this.props.label = this.showItemCat.name;
        //
        //   this.props.value = ids;
        //   // console.log(this.showItemCat);
        //   // this.showItemCat.
        // }).catch(resp=>{
        // });
      }
    },
    //提交表单
    editSubmit: function () {
      // console.log(this.showItemCat);
      this.$confirm('确认提交吗？', '提示', {}).then(() => {
        this.editLoading = true;
        // NProgress.start();
        // this.showItemCat.id = parseInt(ids[ids.length-1]);
        // var id = this.showItemCat.id[this.showItemCat.id.length - 2];
        console.log(this.showItemCat);
        var temp = this.showItemCat.parentId;
        this.temData.typeId = this.showItemCat.typeId;
        this.temData.id = this.showItemCat.id;
        if (temp.length > 0) {
          this.temData.parentId = temp[temp.length - 1];
        } else {
          this.temData.parentId = 0;
        }
        this.temData.name = this.showItemCat.name;
        this.temData.parentPath = '';
        console.log(this.temData);
        axios.post("/itemcat/save", this.temData).then(resp => {
          console.log(resp);
          if (resp.data.code === "1") {
            this.editFormVisible = false;
            this.$message({
              message: '保存成功',
              type: 'success'
            });
            this.editLoading = false;
            this.queryBaran();
          }
        }).catch(resp => {

        })
      });
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.queryBaran();
    },

    selsChange: function (sels) {
      console.log(sels);
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      // var ids = this.sels.map(item => item.id).toString();
      this.sels.forEach(item => this.ids.push(item.id));
      console.log(this.ids);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        // let para = { ids: ids };
        this.$http.patch("/itemcat/deleteBatch",this.ids).then((resp) => {
          if (resp.data.code==="1") {
            this.$message({
              message: '批量删除成功',
              type: 'success'
            });
            this.listLoading = false;
            this.queryBaran();
          }
        });
      }).catch(() => {

      });
    },
    find() {
      console.log(this.$refs.multipleTable.getCheckedProp());
    },
  },
  mounted() {
    this.queryBaran();
    this.queryAllTemplate();
  },
};
</script>

<style scoped lang="scss">
</style>